<?php
/*****************************************
Wer Wird Millionär
TIT10NS Webengeneering Projekt

newquestion - Seite um neue Fragen anzulegen

Autoren: Markus Schwarz
Copyright: 2012/2013 DHBW Mannheim
*****************************************/
include ("inc/startup.php");
include ("inc/header.php");
require ("inc/TAdminNotification.php");
require ("inc/TAdminView.php");
			
if (isset($_POST['categoryButton'])) {
	
	if (isset($_POST['newCategory'])) {
		$category = new TAdminCategory("new", $_POST['newCategory']);
		
		if (TModel::checkCategoryName($category)) {
			$notification = new TAdminNotification ("error", "newCategoryFormNameGiven");		  
			TAdminView::getNewCategoryForm($notification);
		}
		else {
			$catID = TModel::newCategory($category);
			$category->setCategoryID($catID);
			
			$notification = new TAdminNotification ("success", "newCategoryFormInsertSuccess");
			TAdminView::getCategoryForm($category, $notification);
		}
	}
	else {
		$notification = new TAdminNotification ("error", "newCategoryFormTextEmpty");
		TAdminView::getNewCategoryForm($notification);
	}
}
else {
	if (isset($_POST['catButton'])) {
		if ($_POST['catName'] != "") {
			if (TModel::getCategoryByCategoryID($_POST['catID']) != $_POST['catName']) {
				$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
				if (TModel::checkCategoryName($category)) {
					$category = new TAdminCategory($_POST['catID'], "");
					$notification = new TAdminNotification("error", "newCategoryFormNameGiven");

					TAdminView::getCategoryForm($category, $notification);
				}
				else {
					$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
					$questions = $_POST['questions'];
					
					TModel::updateCategory($category, $questions);
					
					$notification = new TAdminNotification("success", "newCategoryFormUpdateSuccess");
					TAdminView::getCategoryForm($category, $notification);
				}
			}
			else {
				// Keine Änderung am Namen
				$category = new TAdminCategory($_POST['catID'], $_POST['catName']);
				$questions = $_POST['questions'];

				TModel::updateCategory($category, $questions);
					
				$notification = new TAdminNotification("success", "newCategoryFormUpdateSuccess");
				TAdminView::getCategoryForm($category, $notification);
			}
		}
		else {
			$category = new TAdminCategory($_POST['catID'], "");
			$notification = new TAdminNotification("error", "newCategoryFormTextEmpty");

			TAdminView::getCategoryForm($category, $notification);
		}
	}
	else {
		$notification = new TAdminNotification ("", "");
		TAdminView::getNewCategoryForm($notification);
	}
}

include ("inc/footer.php");
?>
